function findUserAttendances() {
    var uId = $("#on_job_page_user_id").val();
    var y = $("#a_year").val();
    var m = $("#a_month").val();
    var d = $("#a_day").val();
    var $tb = $("#user_attendance_table").find("tbody");
    $.post("../attendance/findAttendances",
        {"year": y, "month": m, "day": d, "userId": uId},
        function (data) {
            if ($.isEmptyObject(data)) {
                $tb.html("").append("<tr><td colspan='5' style='text-align: center'>无相关消息！</td></tr>")
            } else {
                $tb.html("");
                for (var i = 0, n = data.length; i < n; i++) {
                    var ot = data[i].offTime;
                    $tb.append("<tr><td>" + new Date(data[i].goTime).Format("yyyy-MM-dd hh:mm:ss") + "</td>" +
                        "<td>" + (ot === null ? "还未签退" : new Date(ot).Format("yyyy-MM-dd hh:mm:ss")) + "</td>" +
                        "<td>" + data[i].beLateTimes + "分钟</td>" +
                        "<td>" + data[i].leaveEarlyTimes + "分钟</td>" +
                        "<td>" + data[i].extraWorkTimes + "小时</td></tr>");
                }
                $tb.find("td").css("text-align", "center");
            }
        });
    $.post("../attendance/getOnJobDays",
        {"year": y, "month": m, "userId": uId},
        function (data) {
            $("#user_in_job").html("<b>" + data + "</b>");
        });
}

function findUserRP() {
    var uId = $("#on_job_page_user_id").val();
    var y = $("#rp_year").val();
    var m = $("#rp_month").val();
    var $tb = $("#user_rp_table").find("tbody");
    $.post("../rp/findRPList",
        {"year": y, "month": m, "userId": uId},
        function (data) {
            if ($.isEmptyObject(data)) {
                $tb.html("").append("<tr><td colspan='4' style='text-align: center' '>无相关消息！</td></tr>")
            } else {
                $tb.html("");
                for (var i = 0, n = data.length; i < n; i++) {
                    $tb.append("<tr><td>" + data[i].cause + "</td>" +
                        "<td>" + new Date(data[i].rpDate).Format("yyyy-MM-dd") + "</td>" +
                        "<td>" + data[i].money + "</td>" +
                        "<td>" + (data[i].rewardOrPunishment === 1 ? "奖" : "罚") + "</td></tr>");
                }
            }
            $tb.find("td").css({"text-align": "center", "margin": "3px 15px"});
        });
}

function dissentReset() {
    if (confirm("您确定要放弃处理并返回吗？")) {
        $("#user_dissent_background").hide();
        $("#user_dissent").hide();
    }
}

function checkDissentForm() {
    var val = $("#deal").val();
    if (val === '') {
        alert("请输入异议原因再提交！");
        return false;
    }
    return true;
}

function findUserSalary() {
    var uId = $("#on_job_page_user_id").val();
    var y = $("#salary_year").val();
    var m = $("#salary_month").val();
    var $tb = $("#user_salary_table").find("tbody");
    $.post("../salary/findSalaryList",
        {"userId": uId, "year": y, "month": m},
        function (data) {
            if ($.isEmptyObject(data)) {
                $tb.html("").append("<tr><td colspan='7' style='text-align: center'>无相关消息！</td></tr>")
            } else {
                $tb.html("");
                for (var i = 0, n = data.length; i < n; i++) {
                    $tb.append("<tr><td>" + data[i].finalPay + "</td>" +
                        "<td>" + data[i].entryTable.positionLevel.regularPay + "</td>" +
                        "<td>" + data[i].performance.performance + "</td>" +
                        "<td>" + data[i].extraWorkPay + "</td>" +
                        "<td>" + data[i].rewardPunishment + "</td>" +
                        "<td>" + data[i].entryTable.positionLevel.socialSecurity + "</td>" +
                        "<td class='dissent_td'><input type='hidden' name='ssId' value='" + data[i].id + "'>我有异议</td></tr>");
                }
                $tb.find("td").css({"text-align": "center", "margin": "2px 15px"});
                var $dissent = $tb.find("td[class=dissent_td]");

                $dissent.hover(function () {
                    $(this).css({"cursor": "pointer", "color": "rgba(0,245,120,0.8)"});
                }, function () {
                    $(this).css("color", "rgba(0,20,230,0.7)");
                });

                $dissent.click(function () {
                    var ssId = $(this).find("input[name=ssId]").val();
                    $("#dissent_form").find("input[name='salarySheet.id']").val(ssId);
                    $("#user_dissent_background").show();
                    $("#user_dissent").show();
                });
            }
        });
}

function checkSignIn() {
    var uId = $("#on_job_page_user_id").val();
    var y = new Date().getFullYear();
    var m = new Date().getMonth() + 1;
    if (m < 10)
        m = "0" + m;
    var d = new Date().getDate();
    if (d < 10)
        d = "0" + d;

    console.log("uId = " + uId + "-----year = " + y + "---month = " + m + "---day = " + d);
    $.post("../attendance/findAttendances",
        {"year": y, "month": m, "day": d, "userId": uId},
        function (data) {
            var $usi = $("#main_sign_in");
            var $uso = $("#main_sign_out");
            if ($.isEmptyObject(data)) {
                $usi.attr("disabled", false);
                $uso.attr("disabled", false);
            } else {
                $usi.attr("disabled", true);
                for (var i = 0, n = data.length; i < n; i++) {
                    if (data[i].offTime !== null) {
                        $uso.attr("disabled", true);
                    }
                }
            }
        });
}

function userSignIn() {
    var rId = $("#on_job_page_resume_id").val();
    var now = new Date();
    var times = getTimes(now, "09:00:00");

    $.post("../attendance/userSignIn",
        {"resume.id": rId, "goTime": now.Format("yyyy-MM-dd hh:mm:ss"), "times": times},
        function (data) {
            if (data === 1) {
                $("#main_sign_in").attr("disabled", true);
                if (times > 0)
                    alert("您已迟到，要按时打卡哦！祝您工作愉快！");
                else
                    alert("签到成功，祝您工作愉快！");
            } else {
                alert("签到失败，请重试！");
            }
        });
}

function userSignOut() {
    var rId = $("#on_job_page_resume_id").val();

    var b = $("#main_sign_in").attr("disabled");

    if (typeof(b) === "undefined" || $("#main_sign_in").attr("disabled") === false) {
        alert("您还没有上班打卡呦，请先打上班卡呦！");
    } else {
        var now = new Date();
        var times = getTimes(now, "18:00:00");
        $.post("../attendance/userSignOut",
            {"resume.id": rId, "offTime": now.Format("yyyy-MM-dd hh:mm:ss"), "times": times},
            function (data) {
                if (data === 1) {
                    $("#main_sign_out").attr("disabled", true);
                    if (times < 0)
                        alert("要按时上下班哦！祝您生活愉快！");
                    else
                        alert("签退成功，祝您生活愉快！");
                } else {
                    alert("签退失败，请重试！");
                }
            });
    }
}

function lastDissent() {
    var uId = $("#on_job_page_user_id").val();
    var $tb = $("#user_dissent_table").find("tbody");
    $.post("../dissent/lastDissent",
        {"userId": uId},
        function (data) {
            if ($.isEmptyObject(data))
                $tb.html("").append("<tr><td colspan='7' style='text-align: center'>暂无消息！</td></tr>");
            else {
                $tb.html("");
                $tb.append("<tr><td>" + data.id + "</td>" +
                    "<td>" + data.salarySheet.id + "</td>" +
                    "<td>" + new Date(data.salarySheet.grantDate).Format("yyyy-MM") + "</td>" +
                    "<td>" + data.dissent + "</td>" +
                    "<td>" + (data.looked === 1 ? "已查看" : "未查看") + "</td>" +
                    "<td>" + (data.deal === null ? "未处理" : "已处理") + "</td>" +
                    "<td>" + (data.deal === null ? "未处理" : data.deal) + "</td></tr>");
                $tb.find("td").css("text-align", "center");
            }
        });
}

function getTimes(signTime, timeStr) {

    var y = signTime.getFullYear();

    var m = signTime.getMonth() + 1;
    if (m < 10)
        m = "0" + m;

    var d = signTime.getDate();
    if (d < 10)
        d = "0" + d;

    var dateTimeStr = y + "-" + m + "-" + d + " " + timeStr;

    var signTimes = signTime.getTime();    //打卡时间
    var normalTimes = stringToDateTime(dateTimeStr).getTime(); //正常上班时间

    return Math.floor((signTimes - normalTimes) / 1000 / 60);
}